package com.sun.wanandroid.ui.main

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Category
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Person
import androidx.compose.material.icons.filled.ShoppingCart
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.navArgument
import com.sun.module_base.model.AppThemeState
import com.sun.module_base.serviceloader.MyServiceLoader
import com.sun.module_base.utils.fromJson
import com.sun.module_common.model.WebModel
import com.sun.module_common.route.RouteName
import com.sun.module_common.route.RouteUtils
import com.sun.module_common.serviceinterface.home.IndexService
import com.sun.module_common.serviceinterface.home.QuestionsService
import com.sun.module_common.serviceinterface.home.SearchService
import com.sun.module_common.serviceinterface.home.SquareService
import com.sun.module_common.serviceinterface.hot.AnimationService
import com.sun.module_common.serviceinterface.main.CategoryService
import com.sun.module_common.serviceinterface.main.HomeService
import com.sun.module_common.serviceinterface.main.HotService
import com.sun.module_common.serviceinterface.main.MineService
import com.sun.module_common.serviceinterface.mine.PinLockService
import com.sun.module_common.webview.WebScreen
import com.sun.wanandroid.ui.login.LoginScreen
import com.sun.wanandroid.ui.login.RegisterScreen

/**
 *@author:sunc
 *@time :2025/7/25 14:28
 *@desc: 描述
 */

sealed class NavigationItem(
    var route: String,
    var icon: ImageVector,
    var title: String,
    var messageCount: Int = 0
) {

    object Home : NavigationItem(RouteName.Home.route, Icons.Filled.Home, "首页")
    object Hot : NavigationItem(RouteName.Hot.route, Icons.Default.ShoppingCart, "热门")
    object Category : NavigationItem(RouteName.Category.route, Icons.Filled.Category, "分类")
    object Mine : NavigationItem(RouteName.Mine.route, Icons.Filled.Person, "我的")
}

@Composable
fun Navigation(
    modifier: Modifier = Modifier,
    navController: NavHostController,
    appThemeState: MutableState<AppThemeState>
) {

    /**
     * 设置控制器
     * 使用工具类跳转必须是在这里声明的组件
     */
    RouteUtils.setNavController(controller = navController)


    val routeName = RouteName.Home.route

    NavHost(navController, startDestination = routeName, modifier = modifier) {
        //注意调用invoke
        composable(RouteName.Home.route) {
            MyServiceLoader.load<HomeService>()
                ?.getService(navCtrl = navController, appThemeState = appThemeState)?.invoke()
        } //首页

        composable(RouteName.Hot.route) {
            MyServiceLoader.load<HotService>()?.getService(navCtrl = navController)?.invoke()
        } //热点

        composable(RouteName.Category.route) {
            MyServiceLoader.load<CategoryService>()?.getService(navCtrl = navController)?.invoke()
        }//分类

        composable(RouteName.Mine.route) {
            MyServiceLoader.load<MineService>()?.getService(navCtrl = navController)?.invoke()
        }//我的

        composable(RouteName.Square.route) {
            MyServiceLoader.load<SquareService>()?.getService(navCtrl = navController)?.invoke()
        }//广场

        composable(RouteName.Index.route) {
            MyServiceLoader.load<IndexService>()?.getService(navCtrl = navController)?.invoke()
        }//体系

        composable(RouteName.Questions.route) {
            MyServiceLoader.load<QuestionsService>()?.getService(navCtrl = navController)?.invoke()
        }//问答

        composable(RouteName.Login.route) { LoginScreen() }//登录

        composable(RouteName.Register.route) { RegisterScreen() } //注册

        composable(
            RouteName.Web.route + "?webModel={webModel}", arguments = listOf(
                navArgument("webModel") {
                    type = NavType.StringType
                    defaultValue = ""
                    nullable = false
                }
            )) {
            val json = it.arguments?.getString("webModel") ?: ""
            val bean = json.fromJson<WebModel>()
            WebScreen(webData = bean) { RouteUtils.back() }
        } //web

        composable(RouteName.Search.route) {
            MyServiceLoader.load<SearchService>()?.getService(navCtrl = navController)?.invoke()
        } //搜索

        composable(RouteName.PinLock.route) {
            MyServiceLoader.load<PinLockService>()?.getService(navCtrl = navController)?.invoke()
        } //锁屏

        composable(RouteName.Animation.route) {
            MyServiceLoader.load<AnimationService>()?.getService(navCtrl = navController)?.invoke()
        } //动画页

    }
}

/**
 * 底部导航栏
 */
@Composable
fun BottomNavigationBar(navController: NavController,currentRoute:String?) {
    val items = listOf(
        NavigationItem.Home, NavigationItem.Category, NavigationItem.Hot,
        NavigationItem.Mine
    )
//    val navBackStackEntry by navController.currentBackStackEntryAsState()
//    val currentDestination = navBackStackEntry?.destination

    Column {
        HorizontalDivider()

        NavigationBar(
            modifier = Modifier.height(56.dp),
            tonalElevation = 0.dp //移除默认的阴影增加可用空间
        ) {
            items.forEach { item ->
                NavigationBarItem(
//                    alwaysShowLabel = false,
                    icon = {
                        Icon(
                            item.icon,
                            contentDescription = null,
                            modifier = Modifier.size(20.dp)
                        )
//                        Column(
//                            verticalArrangement = Arrangement.spacedBy(4.dp),
//                            horizontalAlignment = Alignment.CenterHorizontally
//                        ) {
//                            Icon(item.icon, contentDescription = null, modifier = Modifier.size(20.dp))
//                            Text(item.title, fontSize = 12.sp) //手动添加标签
//                        }
                    },
                    label = { Text(item.title, fontSize = 10.sp, color = MaterialTheme.colorScheme.onSurface) },
//                    selected = currentDestination?.hierarchy?.any { it.route == item.route } == true,
                    selected = currentRoute == item.route,
                    onClick = {
                        if (currentRoute != item.route) {
                            navController.navigate(item.route) {
                                popUpTo(navController.graph.findStartDestination().id) {
                                    saveState = true
                                }
                                launchSingleTop = true
                                restoreState = true
                            }
                        }
                    },
                    modifier = Modifier
                        .height(IntrinsicSize.Min)
                        .padding(vertical = 4.dp),
                )
            }
        }
    }
}